package com.netx.generics.translation;
import java.io.Reader;
import java.io.BufferedReader;
import java.io.IOException;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;

import com.netx.generics.basic.Checker;


public abstract class XMLScanner extends FirstStep<Reader> {

	protected XMLScanner(Reader reader) {
		super((Reader)Checker.checkNull(reader, "reader"));
	}
	
	public Object performWork(Results r) {
		try {
			Reader reader = getInitialObject();
			if(!(reader instanceof BufferedReader)) {
				reader = new BufferedReader(reader);
			}
			Document config = new SAXReader().read(reader);
			reader.close();
			return config;
		}
		catch(DocumentException de) {
			r.addError(de.getNestedException().getMessage());
		}
		catch(IOException io) {
			r.addError(io.getMessage());
		}
		return null;
	}
}
